package org.automated.realestatemanager.parsers;

import java.util.Set;
import java.util.SortedSet;

import org.apache.log4j.Logger;
import org.automated.realestatemanager.dataanaliser.Analyzer;
import org.automated.realestatemanager.db.entity.Advertisement;
import org.automated.realestatemanager.db.entity.Agency;
import org.automated.realestatemanager.db.entity.Phone;
import org.automated.realestatemanager.db.entity.Publisher;
import org.automated.realestatemanager.db.services.ServiceFactory;
import org.automated.realestatemanager.parserutils.ParserUtils;

public class ByeFlatAdvertisementParser implements AdvertisementParser {

	private static final Logger log = Logger
			.getLogger(ByeFlatAdvertisementParser.class);

	int counter = 0;
	Advertisement advertisement = null;
	Publisher publisher = null;
	String source = "";

	public void executeParcing(String text) {
		if (text.replaceAll("\\s", "").isEmpty()) {
			log.info("Empty string");
			return;
		}
		counter++;
		log.info("Parsing advertiusement #" + counter + " " + text);

		advertisement = new Advertisement();
		advertisement.setSourse(source);

		Set<Phone> phones = ParserUtils.parsePhones(text);
		String agency = ParserUtils.parseAgency(text);
		Agency ag = null;
		if (!"N/A".equals(agency) && agency != null) {
			advertisement.setAgency(ParserUtils.parseAgency(agency));
			ag = ServiceFactory.getAgencyService().getUniqueResult(
					new Agency(agency));
			if (ag == null)
				ag = ServiceFactory.getAgencyService().create(
						new Agency(agency));
		}

		publisher = Analyzer.getPhonesOwner(phones);

		phones = ServiceFactory.getInstance().getPhoneService()
				.persistPhones(phones);

		if (phones.size() == 0)
			return;

		boolean publisherExist = false;
		if (publisher == null) {
			log.debug("Publisher not found");
			publisher = new Publisher();
			publisher.setName(ParserUtils.parseName(text));
			publisher.setAgency(ag);
		} else {
			log.debug("Publisher found");
			publisherExist = true;
		}

		advertisement.setPhones(phones);
		advertisement.setRooms(ParserUtils.parseRooms(text));
		advertisement.setArea(ParserUtils.parseArea(text));
		advertisement.setFloor(ParserUtils.parseFloor(text));
		advertisement.setPrice(ParserUtils.parsePrice(text));
		advertisement.setIdFromSite(ParserUtils.parseId(text));
		advertisement.setText(ParserUtils.parseText(text));
		advertisement.setWebSite(ParserUtils.parseWebSite(text));
		if ("".equals(advertisement.getArea())
				&& "".equals(advertisement.getFloor())
				&& "".equals(advertisement.getPrice())
				&& "".equals(advertisement.getRooms())) {
			return;
		} else {
			ServiceFactory.getInstance().getAdvertisementService()
					.create(advertisement);
			SortedSet<Advertisement> publAdv = ServiceFactory.getInstance()
					.getAdvertisementService().getAdvSetByPublisher(publisher);
			if (publAdv.contains(advertisement))
				return;
			log.debug("Advertisement saved");
		}

		publisher.getPhones().addAll(phones);

		publisher.getAdvertisements().add(advertisement);
		if (publisherExist) {
			ServiceFactory.getInstance().getPublisherService().merge(publisher);
		} else {
			ServiceFactory.getInstance().getPublisherService()
					.create(publisher);
		}

		log.debug("Publisher saved");
		phones.clear();
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setName(String name) {
		// TODO Auto-generated method stub

	}

	public void setStatus(String string) {
		// TODO Auto-generated method stub

	}

}
